package edu.umn.ftpmaster;

import java.io.File;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.sql.*;
/**
 * for each event, there is a list of ftp clients and one ftp server
 * the ftp clients are used to push files to the mobile
 * the ftp server is used to store all the files 
 * @author Chonglei
 *
 */
public class Event {
	
	private Map<ftpClient,Account> clients = new HashMap<ftpClient,Account>();
	public ftpServer server;
	
	public Location_stp locl;
	public String name;
	public int port;
	public int count =0;
	public double radius = 1.0;
	public double expTime;
	public int eventid;
	
	public Event(Location_stp ll,String name, String path, int newport, int x)
	{
		File dirFile =new File(path);
		if(!(dirFile.exists()) || !(dirFile.isDirectory()))
		{
			dirFile.mkdir();
		}
		server = new ftpServer(newport,path);
		locl =ll;
		this.name = name;
		this.port=newport;
		eventid = x;
		server.startServer();
	}
	/**
	 * set up the scope
	 * @param spec 
	 */
	public void setScope(double scope)
	{
		this.radius = scope;
	}

	/*viswanath: Should this function be located in the mobile side */	
	/*public ftpClient addClient(Account account,InetAddress addr)
	{
		ftpClient client=new ftpClient(true);
		
		System.out.println("IP:"+addr.getHostAddress());
		
		//client.setServerAddress(addr.getHostAddress());
		clients.put(client, account);
		return client;
	}*/
	public String getName()
	{
		return name;
	}

	public static void add_jdbc_event(String creator, String laddress, int eid, String eventName, Location_stp ll, int port, String path)
	{
		Connection db = null;
		Statement sql;                //Our SQL statement
		try {
			db = DriverManager.getConnection("jdbc:postgresql://127.0.0.1/peacock","stfs", "sparrow"); //the port number was 5432
 
		}
		catch(SQLException e){
			System.out.println("Connection Failed! Check output console");
			e.printStackTrace();
			return;
		}
 
		if (db != null){
			try{
				//sql = db.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);

				/*ResultSet results = sql.executeQuery("select * from person where person_name like '" + creator + "';");
				if(results.first())
				{
					String sqltext = "insert into event values ('"+laddress+"',"+eid+", ST_GeomFromEWKT('SRID=-1;POINT("+ll.latitude+" "+
					ll.longitude+" "+ll.time+")'),"+results.getObject(2)+",'"+eventName+"',"+port+",'"+path+"');";
				
					sql.executeUpdate(sqltext);

					// jdbc connection to insert into the people_event table 
			
					db.close();
                                        db = DriverManager.getConnection("jdbc:postgresql://127.0.0.1/peacock","stfs", "sparrow");
                                        sql = db.createStatement();

					sqltext = "insert into event 	
	
				} */
				//else
				//{
					//db.close();
					db = DriverManager.getConnection("jdbc:postgresql://127.0.0.1/peacock","stfs", "sparrow");
					sql = db.createStatement();

					ResultSet results1 = sql.executeQuery("select count(*) from person;");	
					results1.next();
					int new_pid;
					new_pid = results1.getInt(1);	
					new_pid++;

					db.close();
                                        db = DriverManager.getConnection("jdbc:postgresql://127.0.0.1/peacock","stfs", "sparrow");
                                        sql = db.createStatement();
					String sqltext = "insert into person (person_name,person_id) values ('"+creator+"',"+new_pid+");";
					sql.executeUpdate(sqltext);
				
					db.close();
					double scale_down_time = (double)ll.time/(double)3600000.0;
                                        db = DriverManager.getConnection("jdbc:postgresql://127.0.0.1/peacock","stfs", "sparrow");
                                        sql = db.createStatement();	
					String sqltext1 = "insert into event values ('"+laddress+"',"+
                                        eid+", ST_GeomFromEWKT('SRID=-1;POINT("+ll.latitude+" "+
                                        ll.longitude+" "+scale_down_time+")'),"+new_pid+",'"+eventName+"',"+port+",'"+path+"',"+ll.time+");";
					sql.executeUpdate(sqltext1);

					/* jdbc connection to insert into the people_event table */
				
					db.close();
                                        db = DriverManager.getConnection("jdbc:postgresql://127.0.0.1/peacock","stfs", "sparrow");
                                        sql = db.createStatement();
					String sqltext2 = "insert into people_event values ("+new_pid+","+eid+");";
					sql.executeUpdate(sqltext2);					
					db.close();
				//}

			}
			catch(Exception e)
			{
				System.out.println("Sql execution failed in Event.java");
				System.out.println(e);
			}						
		}
		else{
			System.out.println("Failed to make connection!");
		}
	}  
	


}
